<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
		<script src="/js/plugins/dialog2/dialog.min.js"></script>
		<script src="/js/plugins/form/jquery.form.js"></script>
        <script>
            $(function () {
                //使用jrender对我的功能页面的信息回显
                var user= JSON.parse(sessionStorage.getItem("user"));
                //渲染用户信息
                $(".info").renderValues(user);
                //背景图片需要单独回显
                $(".myBg").css("backgroundImage", "url('" + user.coverImgUrl + "')");
                //绑定infoForm的提交action
                $("#infoForm").attr("action", "/users/" + user.id);
                //绑定保存按钮提交事件
                $("#saveBtn").click(function () {
                    //发送put请求
                    $("#infoForm").ajaxSubmit(function (data) {
                        if (data.success) {
                            //保存成功，跳转回我的功能页面
                            console.log(data.result);
                            sessionStorage.setItem("user", JSON.stringify(data.result));
                            window.location.href = "/mine/profiles.html";
                        }
                    });
                });
                //引入变量，表示当前文件编辑的实际对象
                var flag = true;//默认头像
                //绑定头像编辑按钮，代码实现点击文件筐
                $("#headBtn").click(function () {
                    $("#fileInput").click();
                    flag = true;
                });
                //绑定背景编辑按钮，代码实现点击文件筐
                $("#coverBtn").click(function () {
                    $("#fileInput").click();
                    flag = false;
                });

                //文件框绑定值改变事件，进行表单的提交，提交成功后回显
                $("#fileInput").change(function () {
                   if($(this).val()) {
                       $("#fileForm").ajaxSubmit(function (data) {
                           //提交成功后回显到头像或背景
                           if (data.status) {
                               if (flag) {
                                   //回显到头像
                                   $("#headImg").attr("src",data.url);
                                   //设置到隐藏域
                                   $("input[name=\"headImgUrl\"]").val(data.url);
                               }else {
                                   //回显到背景
                                   $(".myBg").css("backgroundImage", "url('" + data.url + "')");
                                   //设置到隐藏域
                                   $("input[name=\"coverImgUrl\"]").val(data.url);
                               }
                           }else{
                               //保存失败
                               $(document).dialog({
                                   titleText: '温馨提示',
                                   content: data.msg
                               });
                           }
                       });
                   }
                });
            });
        </script>
	</head>

	<body>
    <form action="/images" method="post" enctype="multipart/form-data" id="fileForm">
        <!--隐藏文件框，用于上传头像及背景-->
        <input id="fileInput" type="file" name="file" style="display: none">
    </form>
		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<!--跳转到上一页-->
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a role="button" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg" >
				<img id="headImg" render-src="headImgUrl" class="rounded-circle"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post" >
                    <input type="hidden" name="_method" value="put">
                    <!--使用隐藏域，传递头像和封面的信息-->
					<input type="hidden" name="headImgUrl" render-value="headImgUrl" >
					<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">

					<label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName">
					<label>性别</label><select class="form-control" name="gender" render-value="gender">
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input type="text" name="place"  class="form-control" render-value="place">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
